package cn.stylefeng.guns.modular.business.frequency.ordersub.controller;

import cn.stylefeng.guns.core.factory.PageFactory;
import cn.stylefeng.guns.core.pojo.response.ResponseData;
import cn.stylefeng.guns.modular.business.custom.enums.OrderStatusEnum;
import cn.stylefeng.guns.modular.business.frequency.finance.service.FinanceService;
import cn.stylefeng.guns.modular.business.frequency.orderpick.service.OrderPickService;
import cn.stylefeng.guns.modular.business.frequency.ordersub.model.param.OrderDTO;
import cn.stylefeng.guns.modular.business.frequency.ordersub.model.result.PkgTake;
import cn.stylefeng.guns.modular.business.frequency.ordersub.service.TakeCarService;
import cn.stylefeng.guns.modular.business.frequency.ordertake.entity.OrderTake;
import cn.stylefeng.guns.modular.business.frequency.statistics.PkgOrderTake;
import cn.stylefeng.guns.modular.business.util.BusinessUtil;
import cn.stylefeng.guns.modular.util.BaseUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Author jzy
 * @Date 2021/2/5 0005
 **/
@Api(tags = "订单模块：送车管理")
@RestController
@RequestMapping("/api/take")
public class TakeCarController {

    @Resource
    private TakeCarService takeCarService;
    @Resource
    private OrderPickService pickCarService;
    @Resource
    private FinanceService financeService;
    @Resource
    private BusinessUtil businessUtil;

    @ApiOperation(value = "待送车分页列表")
    @GetMapping("/wait")
    public ResponseData getWaitTakeList(OrderDTO orderDTO) {
        orderDTO.setStatusOnly(OrderStatusEnum.FINISHED_TRANSPORT.status);
        getOrderTakeQueryParam(orderDTO);
        if (StringUtils.isBlank(orderDTO.getColumn())) {
            orderDTO.setColumn("orderId");
        }
        return takeCarService.getWaitTakeList(PageFactory.defaultPage(), orderDTO);
    }

    @ApiOperation(value = "待指派司机分页列表")
    @GetMapping("/wait-driver")
    public ResponseData getWaitAssignDriverList(OrderDTO orderDTO) {
        orderDTO.setStatusOnly(OrderStatusEnum.WAIT_ASSIGN_TAKE_DRIVER.status);
        getOrderTakeQueryParam(orderDTO);
        return takeCarService.getWaitTakeList(PageFactory.defaultPage(), orderDTO);
    }

    @ApiOperation(value = "送车信息详情")
    @GetMapping("/detail/{orderId}")
    public ResponseData getTakeCarDetail(@PathVariable Long orderId) {
        PkgTake ot = takeCarService.getTakeCarDetail(orderId);
        return ResponseData.success(ot);
    }

    @ApiOperation(value = "批量指派送车")
    @PostMapping
    public ResponseData assignTake(@Validated @RequestBody OrderTake orderTake) {
        StringBuilder errorMsg = new StringBuilder();
//        boolean hasAssignDriver = BaseUtils.hasAssignDriver(orderTake.getDriverId(), orderTake.getDriverName()
//                , orderTake.getDriverPhone(), orderTake.getDriverCardNumber(), orderTake.getLicensePlateNumber());
        if (!takeCarService.checkAssignTake(errorMsg, orderTake)) {
            return ResponseData.andaError(errorMsg.toString());
        }
        Map<String, String> map = pickCarService.getSettlementInfo(orderTake.getSettlementId(), orderTake.getSettlementName(), orderTake.getSettlementPhone(),
                orderTake.getSettlementType(),
                orderTake.getOrderId() == null ? orderTake.getTakeOrderVOList().get(0).getOrderId() : orderTake.getOrderId(),
                2);
        takeCarService.multiTake(orderTake, map);
//        if (hasAssignDriver) {
        return ResponseData.andaSuccess();
//        } else {
//            return ResponseData.andaSuccess("注意：司机信息不完整，订单将进入待指派司机状态，后续可以去待指派司机页面继续完善司机信息完成提车！");
//        }
    }

    @ApiOperation(value = "回退指派送车")
    @GetMapping("/reTake/{orderId}")
    public ResponseData reTake(@PathVariable Long orderId) {
        takeCarService.reMultiTake(orderId);
        return ResponseData.andaSuccess();

    }

    //@NoRepeatSubmit
    @ApiOperation(value = "指派送车编辑")
    @PutMapping
    @PreAuthorize("@el.check('pickcar:edit')")
    public ResponseData updateAssignTake(@Validated @RequestBody OrderTake orderTake) {
        StringBuilder errorMsg = new StringBuilder();
        if (!takeCarService.checkAssignUpdate(orderTake, errorMsg)) {
            return ResponseData.andaError(errorMsg.toString());
        }
        takeCarService.updateAssign(orderTake);
        // finance表状态修改
        financeService.changeFinanceStatus(orderTake.getOrderId());
        return ResponseData.andaSuccess();
    }

    @ApiOperation(value = "添加待送车弹框筛选")
    @GetMapping("/wait-exclude")
    public ResponseData getWaitTakeExclude(OrderDTO orderDTO) {
        // 数据权限
        orderDTO.setExcludeOrderIds(orderDTO.getExcludeOrderIds());
        getOrderTakeQueryParam(orderDTO);
        List<PkgOrderTake> list = takeCarService.getWaitTakeExcludeList(orderDTO);
        return ResponseData.andaSuccess(list);
    }

    private void getOrderTakeQueryParam(OrderDTO orderDTO) {
        List<Long> empByOrgList = businessUtil.getEmpByOrgList();
        orderDTO.setDataList(empByOrgList);

    }
}
